#include "stack.h"

static bool is_stack_full(const stack_t *pstack) {
  return (pstack->top == pstack->size);
}


static bool is_stack_empty(const stack_t *pstack) {
  return (pstack->top == 0);
}


bool push(stack_t *pstack, int data) {
  if (is_stack_full(pstack)) {
    return false;
  }
  pstack->pdata[pstack->top++] = data;
  return true;
}

bool pop(stack_t *pstack, int *data) {
  if (is_stack_empty(pstack)) {
    return false;
  }
  *data = pstack->pdata[--pstack->top];
  return true;
}


#ifdef TEST_STACK

int main(int argc, char const *argv[]) {
  int buff[10] = {0};

  stack_t stack = _new_stack(buff);

  printf("push: ");
  for (int i = 0; i < 10; i++) {
    push(&stack, i*2+1);
    printf("%02d ", i*2+1);
  }
  printf("\n");

  printf("pop:  ");
  int data;
  while (pop(&stack, &data)) {
    printf("%02d ", data);
  }
  printf("\n");

  return 0;
}

#endif



// 方式1

#if 0

#define STACK_SIZE 100

static int stack[STACK_SIZE] = {0};

static int top = 0;


bool is_stack_full(void) {
  return (top == STACK_SIZE);
}

bool is_stack_empty(void) {
  return (top == 0);
}


bool push(int data) {
  if (is_stack_full()) {
    return false;
  }
  stack[top++] = data;
  return true;
}

bool pop(int *data) {
  if (is_stack_empty()) {
    return false;
  }
  *data = stack[--top];
  return true;
}





int main(int argc, char const *argv[]) {

  printf("push: ");
  for (int i = 0; i < 10; i++) {
    push(i*2+1);
    printf("%02d ", i*2+1);
  }
  printf("\n");


  int data;
  printf("pop:  ");
  while (pop(&data)) {
    printf("%02d ", data);
  }
  printf("\n");


  return 0;
}


#endif